package com.platform.hjt.sys.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.hjt.tools.date.DateCreator;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.costant.ErrorCode;
import com.platform.hjt.base.costant.UserType;
import com.platform.hjt.base.service.BaseService;
import com.platform.hjt.sys.dao.SysDeptDAO;
import com.platform.hjt.sys.model.SysDept;

@Service
public class SysDeptService extends BaseService{

	@Resource
	private SysDeptDAO sysDeptDAO;
	
	public ReturnBody saveSysDept(SysDept sysDept){
		if(StringUtils.isNotBlank(sysDept.getSysDeptId())){
			SysDept oldSysDept = sysDeptDAO.get(sysDept.getSysDeptId());
			
			if(oldSysDept == null){
				return new ReturnBody(false, "删除错误，请联系管理员。错误代码:" + ErrorCode.NoEntity.getKey());
			}
			
			//如果不是管理员需要判断权限
			if(!getSessionUserInfo().getUserType().equals(UserType.MASTER.getKey())){
				if(!oldSysDept.getSysCompanyInfo().getSysCompanyInfoId().equals(getSessionSysCompanyInfo().getSysCompanyInfoId())){
					return new ReturnBody(false, "删除错误，请联系管理员。错误代码:" + ErrorCode.NoPower.getKey());
				}
			}
			//设置公司为空，部门公司不允许修改
			sysDept.setSysCompanyInfo(null);
			if(sysDept.getSysDept() != null && StringUtils.isBlank(sysDept.getSysDept().getSysDeptId()) ){
				sysDept.setSysDept(null);
			}
			
			sysDeptDAO.updatePerproties(sysDept);
			return  new ReturnBody(true, "更新成功");
		}else{
			sysDept.setIsDel(false);
			sysDept.setCreateTime(DateCreator.getCurrentTime());
			
			//如果不是管理员只能添加本公司的部门
			if(!getSessionUserInfo().getUserType().equals(UserType.MASTER.getKey())){
				sysDept.setSysCompanyInfo(getSessionSysCompanyInfo());
			} 
			
			if(sysDept.getSysDept() != null && StringUtils.isBlank(sysDept.getSysDept().getSysDeptId()) ){
				sysDept.setSysDept(null);
			}
			
			sysDeptDAO.saveJ(sysDept);
			return  new ReturnBody(true, "添加成功");
		}
	}
	
	public ReturnBody delSysDept(SysDept sysDept){
		
		sysDept = sysDeptDAO.get(sysDept.getSysDeptId());
		if(sysDept == null){
			return new ReturnBody(false, "删除错误，请联系管理员。错误代码:" + ErrorCode.NoEntity.getKey());
		}
		
		//如果不是管理员需要判断权限
		if(!getSessionUserInfo().getUserType().equals(UserType.MASTER.getKey())){
			if(!sysDept.getSysCompanyInfo().getSysCompanyInfoId().equals(getSessionSysCompanyInfo().getSysCompanyInfoId())){
				return new ReturnBody(false, "删除错误，请联系管理员。错误代码:" + ErrorCode.NoPower.getKey());
			}
		}
		
		sysDept.setIsDel(true);
		sysDeptDAO.updatePerproties(sysDept);
		return  new ReturnBody(true, "删除成功");
	}
	
	public Page<SysDept> getSysDeptList(Page<SysDept> page, List<PropertyFilter> propertyFilterList){
		return sysDeptDAO.findPage(page, PropertyFilter.buildFromHttpRequest(getRequest()));
	}
	
	public List<SysDept> getSysDeptList(List<PropertyFilter> filters){
		return sysDeptDAO.find(filters);
	}
	
	public SysDept getSysDept(List<PropertyFilter> filters){
		return sysDeptDAO.findUniqueBy(filters);
	}
	
	public SysDept getSysDept(String propertyName, Object value){
		return sysDeptDAO.findUniqueBy(propertyName, value);
	}
	
	@SuppressWarnings("all")
	public List<SysDept> getSysDeptListByCompanyIds(List<String> companyIds){
		String hql = "select sysDept from SysDept sysDept where sysDept.isDel=false and  sysDept.sysCompanyInfo.sysCompanyInfoId in (:ids) order by sysDept.sortIndex";
		return sysDeptDAO.getSession().createQuery(hql).setParameterList("ids", companyIds.toArray(new String[companyIds.size()])).list();
	}
}
